package com.usian.article.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sun.tools.javah.LLNI;
import com.usian.apis.article.ApAuthorControllerApi;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/author")
public class ApAuthorController implements ApAuthorControllerApi {

    @Autowired
    private ApAuthorService apAuthorService;

    @Override
    @GetMapping("/findByUserId/{id}")
    public ApAuthor findByUserId(@PathVariable Integer id) {
        List<ApAuthor> list = apAuthorService.list(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, id));
        if (list.size()>0 && list!=null){
            return list.get(0);
        }
        return null;
    }

    @Override
    @PostMapping("/save")
    public ApAuthor save(@RequestBody ApAuthor apAuthor) {
        boolean result = apAuthorService.save(apAuthor);
        if (result){
            return apAuthor;
        }
        return null;
    }

    @Override
    @GetMapping("/findByName/{name}")/*根据自媒体用户表名称查询ap作者*/
    public ApAuthor findByName(@PathVariable String name) {
        return apAuthorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName,name));
    }

    @Override
    @GetMapping("/one/{id}")
    public ApAuthor findById(@PathVariable Integer id) {
        return apAuthorService.getById(id);
    }

    @Override
    @PostMapping("/updateById")
    public ResponseResult updateById(@RequestBody ApAuthor apAuthor) {
        boolean result = apAuthorService.updateById(apAuthor);
        if (result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

}
